package com.lxs.legou.order.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * @Auther: Mcb
 * @Date: 2021/12/12 19:26
 * @Description:
 */
@Configuration
//开启资源服务器
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启方法注解授权配置
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Autowired
    private TokenStore tokenStore;

    /**
     * 配置tokenStore 使用公钥校验令牌
     * @param resources
     * @throws Exception
     */
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.tokenStore(tokenStore);
    }

    /**
     * 配置一些访问地址的权限
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        //跨入禁用
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/user/login","/security/user/login").permitAll() //用户登录可匿名访问
                .antMatchers("/item/**").hasRole("ADMIN")
                .antMatchers("/**").authenticated();
    }


}
